package cn.yuanqiao.web.controller.archive.storeRoom;

import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.archive.storeRoom.domain.Humiture;
import cn.yuanqiao.archive.storeRoom.service.IHumitureService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 温湿度登记Controller
 * 
 * @author xy
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/archive/humiture")
public class HumitureController extends BaseController
{
    @Autowired
    private IHumitureService humitureService;

    @Autowired
    private ITenantSysDictTypeService sysDictTypeService;

    /**
     * 查询温湿度登记列表
     */

    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Humiture humiture)
    {
        startPage();
        List<Humiture> list = humitureService.selectHumitureList(humiture);
        return getDataTable(list);
    }

    /**
     * 导出温湿度登记列表
     */


/*
    @PreAuthorize("@ss.hasPermi('storeroom:humiture:export')")
*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:export')")
    @Log(title = "温湿度登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Humiture humiture)
    {
        List<Humiture> list = humitureService.selectHumitureList(humiture);
        ExcelUtil<Humiture> util = new ExcelUtil<Humiture>(Humiture.class);
        for(int i=0;i<list.size();i++){
            Humiture newHumiture=list.get(i);
            String dictValue = null;
            List<TenantSysDictData> dictDataList = sysDictTypeService.selectDictDataByType("WEATHER");  //WEATHER
            for (TenantSysDictData dict : dictDataList) {
                if (dict.getDictLabel().equals(newHumiture.getWeather()) || dict.getDictValue().equals(newHumiture.getWeather())) {  // 10年   D10
                    dictValue = dict.getDictLabel();
                    break;
                }
            }
            newHumiture.setWeather(dictValue);
            list.set(i,newHumiture);
        }
        util.exportExcel(response, list, "温湿度登记数据");
    }

    /**
     * 获取温湿度登记详细信息
     */

/*
    @PreAuthorize("@ss.hasPermi('storeroom:humiture:query')")
*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:detail')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(humitureService.selectHumitureById(id));
    }

    /**
     * 新增温湿度登记
     */
/*
    @PreAuthorize("@ss.hasPermi('storeroom:humiture:add')")
*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:add')")
    @Log(title = "温湿度登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Humiture humiture)
    {
        return toAjax(humitureService.insertHumiture(humiture));
    }

    /**
     * 修改温湿度登记
     */

/*
    @PreAuthorize("@ss.hasPermi('storeroom:humiture:edit')")
*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:edit')")
    @Log(title = "温湿度登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Humiture humiture)
    {
        return toAjax(humitureService.updateHumiture(humiture));
    }

    /**
     * 删除温湿度登记
     */

/*
    @PreAuthorize("@ss.hasPermi('storeroom:humiture:remove')")
*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:TemperatureHumidity:delete')")
    @Log(title = "温湿度登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(humitureService.deleteHumitureByIds(ids));
    }
}
